/*
 * Copyright (C) <2014> 神马才注册 <373575012@qq.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wen.qqrobot.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * xhj-robot
 *
 * @author 神马才注册
 */
public class Xiaohuangji {

    private static final Logger LOG = Logger.getLogger(Xiaohuangji.class.getName());

    private static final String URL = "http://www.simsimi.com/func/reqN";

    private static final int REPEAT_TIME = 3;

    private static final String[] AD = {"微信", "信号", "新浪", "关注", "微博", "www.", ".com", "扣扣"};

    private static final Map<String, String> PROPERTIES = new HashMap<String, String>() {
        {
            put("Cookie",
                    "simsimi_uid=51678750; "
                    + "Filtering=0.0; "
                    + "sid=s%3ArYalXpIcOh3zAEo0SDWep7TQ.SjEbAoTeYsXMLm9TgzyIGHpge1yrSBzNbcj8Cvvp87A; "
                    + "AWSELB=150F676708F2639057F41EA6CD9115064C58E864E4D5FE3F62AF683EB3CA54C1A44837308BAB86F4F48D2BA2A2B01B0AEA34FBA3D92BA7AB89083051C189504CF5589F0BF7; "
                    + "sim_name=%u5BA2%u4EBA; "
                    + "teach_btn_url=talk; "
                    + "selected_nc=ch;"
            );
            put("Host", "www.simsimi.com");
            put("Referer", "http://www.simsimi.com/talk.htm");
        }
    };

    public static synchronized String chat(String text) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("req", text.trim());
        params.put("lc", "ch");
        params.put("ft", "0.0");
        params.put("fl", "http://www.simsimi.com/talk.htm");
        return chat(params, REPEAT_TIME);
    }

    private static String chat(Map<String, String> params, int repeatTime) {
        if (repeatTime-- == 1) {
            //return "(￣o￣).zZ 对不起，服务器又睡着了~";
            return "你说什么？敢再说一遍？";
        }
        String response = null;
        try {
            response = HttpUtil.doGet(URL, params, PROPERTIES);
            //response = StringUtil.getMiddle(response, "{\"response\":\"", "\",\"id\"");
            response = getResultFromJson(response);
        } catch (IOException ex) {
            return chat(params, repeatTime);
        } catch (JSONException ex) {
            return "不清楚你在说啥。。。";
        }
        if (StringUtil.isEmpty(response)) {
            return "呃。。";
        }
        if (isAd(response)) {
            System.out.println("发现广告: " + response);
            return "嘛~";
        }
        return delTrash(response);

    }

    private static String getResultFromJson(String json) throws JSONException {
        System.out.println(json);
        
        JSONObject jo = new JSONObject(json);
//        if ("OK".equals(jo.getString("msg"))) {
//            return jo.getString("sentence_resp");
//        }
        return jo.getString("sentence_resp");
//        throw new JSONException("Result is not right.");
    }

    private static String delTrash(String text) {
        text = StringUtil.repaceTabs(text);
        return text.replace("\\n", "");
    }

    private static boolean isAd(String text) {
        for (String ad : AD) {
            if (text.contains(ad)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(chat(">_<"));
        Thread.sleep(2000);
        System.out.println(chat("恩?"));
        Thread.sleep(2000);
        System.out.println(chat("你好."));
    }

}
